//********************************************************************
//Project name:	Trabalho 1, Servidor do chat de Teleprocessamento e Redes (2012/1)
//Authors:		Vitor Machado, Wakim Jraige, Wander Soares
//Remarks:		This project uses UTF-8 Encoding
//********************************************************************
package br.ufrj.dcc.tp.servidor;

import java.awt.Component;
import java.io.IOException;

import br.ufrj.dcc.tp.servidor.log.Log;
import br.ufrj.dcc.tp.servidor.log.LogLevel;
import br.ufrj.dcc.tp.servidor.model.Server;
import br.ufrj.dcc.tp.unitTest.driver.MainTest;
import br.ufrj.dcc.tp.utils.Config;
/**
 * Inicializa o servidor. 
 */
public class Main implements MainTest {
	
	private Server servidor;
	private Integer id;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if(args.length > 0) {
			try {
				int port = Integer.parseInt(args[0]);
				Config.DEFAULT_SERVER_PORT.setValue(port);
			} catch(NumberFormatException e) {
			}
		}
		try {
			Server servidor = new Server();
			servidor.start();
		} catch (IOException e) {
			Log.write(Main.class, LogLevel.SEVERE,"Servidor nao pode ser iniciado.");
			Log.write(Main.class, e);
		} catch(IllegalArgumentException e) {
			Log.write(Main.class, LogLevel.SEVERE,"Servidor nao pode ser iniciado.");
			Log.write(Main.class, e);
		}
	}
	
	public void run() {
		try {
			servidor = new Server();
			servidor.start();
		} catch (IOException e) {
			Log.write(Main.class, LogLevel.SEVERE,"Servidor nao pode ser iniciado.");
			Log.write(Main.class, e);
		}
	}
	
	@Override
	public Component getComponent(Class<? extends Component> klass) {
		return servidor.getComponent(klass);
	}

	@Override
	public Object getController(Class<?> klass) {
		return null;
	}

	@Override
	public Integer getId() {
		return id;
	}

	@Override
	public void setId(Integer id) {
		this.id = id;
	}

	@Override
	public void stop() {
		try {
			servidor.shutdown();
			servidor.join();
		} catch (InterruptedException e) {
		}
	}
}
